package com.aeks.divide;

import java.util.HashMap;
//分割
public class Segmentation {
	private String tem = null;//临时字符串
	private HashMap mapDic, len;//词典的 词，词的长度

	public Segmentation(HashMap mapDic, HashMap len) {
		this.mapDic = mapDic;
		this.len = len;
	}

	public String Fmm(String source) {
		String[] targets = new String[source.length()];
		String target = "";
		int MaxLen = source.length();//要分割的数据的最大长度
		int temLen = MaxLen;//高位长度
		int primarylen = 0;//低位长度
		while (true) {
			int count = temLen - primarylen;
			if (len.containsKey(count)) {//是否包含键为temLen值的值,有则返回true
				tem = source.substring(primarylen, temLen);//分割出来的临时字符串
				if (mapDic.containsKey(tem) || temLen - primarylen == 1) {//临时字符串包含在词典中或者高低位之差为1
					primarylen = temLen;
					temLen = MaxLen;
					if (primarylen == MaxLen) {
						target = target + tem;
					} else {//tem为词典的词
						target = target + tem + "/";
					}
				} else {
					temLen--;
				}
			} else {
				temLen--;
			}
			
			if (primarylen == MaxLen){
				break;
			}
		}
		return target;
	}

	/*public String Bmm(String source) {
		String[] targets = new String[source.length()];
		String target="";
		int MaxLen = source.length();
		int temLen = MaxLen;
		int primarylen = 0;
		int i=0;
		while (true) {
			int count = temLen - primarylen;
			if (len.containsKey(count)) {
				tem = source.substring(primarylen, temLen);
				if (mapDic.containsKey(tem)||temLen-primarylen==1) {
					if (temLen == MaxLen){
						targets[i] = tem;
						}
					else{
						tem = tem+"/";
					targets[i] = tem;
					}
					temLen = primarylen;
					primarylen = 0;
					i++;
				} else
					primarylen++;
			} else
				primarylen++;
			if (temLen == 0)
				break;
		}
		
		for(int j=i-1;j>=0;j--)
			target+=targets[j];
		return target;
	}*/

}
